﻿using System;
using System.Collections.Generic;
using System.Text;
using QWeiboSDK;

namespace Method
{
    public class OauthVerify
    {
        private string appKey = null;
        private string appSecret = null;
        private string accessKey = null;
        private string accessSecret = null;
        private string oauthVerify = null;
        private string tokenKey = null;
        private string tokenSecret = null;

        public string AppKey
        {
            get { return appKey; }
            set { appKey = value; }
        }

        public string AppSecret
        {
            get { return appSecret; }
            set { appSecret = value; }
        }

        public string TokenKey
        {
            get { return tokenKey; }
        }

        public string TokenSecret
        {
            get { return tokenSecret; }
        }

        public string AccessKey
        {
            get { return accessKey; }
        }

        public string AccessSecret
        {
            get { return accessSecret; }
        }

        public bool GetRequestToken()
        {
            string url = "https://open.t.qq.com/cgi-bin/request_token";
            List<Parameter> parameters = new List<Parameter>();
            OauthKey oauthKey = new OauthKey();
            oauthKey.customKey = appKey;
            oauthKey.customSecrect = appSecret;
            oauthKey.callbackUrl = "http://www.qq.com";

            QWeiboRequest request = new QWeiboRequest();
            return ParseToken(request.SyncRequest(url, "GET", oauthKey, parameters, null));
        }

        public bool GetAccessToken(string verifier)
        {
            string url = "https://open.t.qq.com/cgi-bin/access_token";
            List<Parameter> parameters = new List<Parameter>();
            OauthKey oauthKey = new OauthKey();
            oauthKey.customKey = appKey;
            oauthKey.customSecrect = appSecret;
            oauthKey.tokenKey = tokenKey;
            oauthKey.tokenSecrect = tokenSecret;
            oauthKey.verify = verifier;

            QWeiboRequest request = new QWeiboRequest();
            if (ParseToken(request.SyncRequest(url, "GET", oauthKey, parameters, null)))
            {
                accessKey = tokenKey;
                accessSecret = tokenSecret;
                return true;
            }
            else
            {
                return false;
            }
        }

        private bool ParseToken(string response)
        {
            if (string.IsNullOrEmpty(response))
            {
                return false;
            }

            string[] tokenArray = response.Split('&');

            if (tokenArray.Length < 2)
            {
                return false;
            }

            string strTokenKey = tokenArray[0];
            string strTokenSecrect = tokenArray[1];

            string[] token1 = strTokenKey.Split('=');
            if (token1.Length < 2)
            {
                return false;
            }
            tokenKey = token1[1];

            string[] token2 = strTokenSecrect.Split('=');
            if (token2.Length < 2)
            {
                return false;
            }
            tokenSecret = token2[1];

            return true;
        }
    }
}
